require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("../../assets/fonts/iconfont.css");
require("./register.less");

// 引入模块
const CaptchaMini = require("captcha-mini");
const dom = require("../../utils/dom.js");
const axios = require("axios");
document.addEventListener("DOMContentLoaded", function () {
	// 验证码
	let captcha1 = new CaptchaMini();
	let codeInit = "";
	captcha1.draw(dom.get("#captcha1"), (r) => {
		codeInit = r.toLowerCase();
	});
	// 表单验证
	dom.get(".reg-btn").addEventListener("click", function () {
		// 手机号验证
		let wong = dom.get("#wong");
		let tell = dom.get("#tel").value;
		let reg = /^1[3456789]\d{9}$/i;
		if (reg.test(tell)) {
			wong.textContent = "";
		} else {
			wong.textContent = "手机号有误";
			return;
		}
		// 验证码
		let code = dom.get("#code").value.toLowerCase();
		if (code === codeInit) {
			wong.textContent = "";
		} else {
			wong.textContent = "验证码有误";
			return;
		}
		// 密码验证
		let pwd = dom.get("#pwd").value;
		if (pwd.length >= 6) {
			wong.textContent = "";
		} else {
			wong.textContent = "密码6位数以上";
			return;
		}
		let pwd2 = dom.get("#pwd2").value;
		if (pwd === pwd2) {
			wong.textContent = "";
		} else {
			wong.textContent = "密码不一致";
			return;
		}
		// 发送请求
		axios
			.post("http://fitness.h5.itsource.cn:3701/api/user/register", {
				account: tell,
				password: pwd,
			})
			.then((res) => {
				console.log(res);
				if (res.data.errno === 0) {
					location.href = "login.html";
					wong.textContent = "注册成功";
				} else {
					wong.textContent = res.data.message;
				}
			});
	});
});
